package com.example.myapplication2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.myapplication2.R;

public class BlankFragment2 extends Fragment {


    @Override
    public void onStart() {
        super.onStart();
        System.out.println("选中:"+getPosition());
        System.out.println("onStart......................开始");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("选中:"+getPosition());
        System.out.println("onCreate......................创建");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("选中:"+getPosition());
        System.out.println("onActivityCreated......................创建的活动成功后");
    }

    @Override
    public void onResume() {
        super.onResume();
        System.out.println("选中:"+getPosition());
        System.out.println("onResume......................重新开始");
    }

    @Override
    public void onPause() {
        super.onPause();
        System.out.println("选中:"+getPosition());
        System.out.println("onPause...............暂停");
    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("选中:"+getPosition());
        System.out.println("onStop...............停止");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("选中:"+getPosition());
        System.out.println("onDestroy...........摧毁");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("选中:"+getPosition());
        System.out.println("onDestroyView.............销毁碎片视图");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("选中:"+getPosition());
        System.out.println("onDetach.........将碎块撕下来");
    }
    //创建视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        System.out.println("onCreateView......................创建视图");
        View inflate = inflater.inflate(R.layout.fragment_blank2, container, false);
        ImageView imageView = inflate.findViewById(R.id.fragment_ig);
        TextView textView = inflate.findViewById(R.id.fragment_text);
        Bundle arguments = getArguments();
        if (arguments!=null){
            String string = arguments.getString("text");
            int anInt = arguments.getInt("ioc");
            imageView.setImageResource(anInt);
            textView.setText(string);
        }
        return inflate;
    }
    //传入参数初始化碎片
    public static BlankFragment2 newInstance(int ioc,String text) {

        Bundle args = new Bundle();
        args.putString("text",text);
        args.putInt("ioc",ioc);
        BlankFragment2 fragment = new BlankFragment2();
        fragment.setArguments(args);
        return fragment;
    }
    private int getPosition(){
        return getArguments().getInt("ioc");
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        System.out.println("选中:"+getPosition());
    }
}